package crp.object;

/**
 * This class represents the correlation matrix which is a
 * symmetric matrix. Because it's symmetric, we only use the 
 * upper triangular part which is a one dimension array.
 * 
 * Notice: the index method used here is:
 * 
 * loc = (j+1) * j / 2 + i (i <= j)
 * 
 * i: the index of 1st dimension
 * j: the index of 2nd dimension
 * 
 * @author ouhang
 */
public class SymmMatrix {

	public double[] symM;
	public int dim;
	
	public SymmMatrix(int dim){
		this.symM = new double[dim*(dim+1)/2];
		this.dim = dim;
	}
	
	public double getValue(int index1, int index2){
		return this.symM[getLocation(index1, index2)];
	}
	
	public void setValue(double value, int index1, int index2){
		this.symM[getLocation(index1, index2)] = value;
	}
	
	private int getLocation(int index1, int index2){
		if(index1 <= index2) return (index2+1)*index2/2+index1;
		return (index1+1)*index1/2+index2;
	}
	/**
	 * for test
	 */
	public void printSelf(){
		for(int i = 0 ; i < dim ; i++){
			System.out.println();
			for(int j = 0 ; j < dim ; j++)
				System.out.print(this.getValue(i, j)+" ");
		}
	}
}

